<template>
  <el-card>
    <div class="table-head">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model="searchText"
            @input="searchChange"
            placeholder="搜索商品"
          ></el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table :data="goodsList" style="width: 100%" border>
      <el-table-column type="index" label="" width="50"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="500">
        <template #default="scope">
          <div class="goods-name">{{ scope.row.goods_name }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="goods_price"
        label="价格(元)"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="goods_number" label="商品数量" align="center">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" align="center">
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="scope">
          {{ formatDate(scope.row.add_time) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEditGoods(scope.row.goods_id)"
            >编辑</el-button
          >
          <el-button class="delete" type="text" size="small"
            ><el-popconfirm
              title="确认要删除吗"
              @confirm="handleDelete(scope.row.goods_id)"
            >
              <template #reference>
                <el-button size="small" type="text" style="color: #db614e"
                  >删除</el-button
                >
              </template>
            </el-popconfirm></el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pages.pagesize"
        :total="pages.pagetotal"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import { GET_GOODSLIST, DELETE_GOODS } from '../../api/request'
import { onMounted, reactive, toRefs, ref } from 'vue'
import { formatDate } from '../../plugins'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export default {
  setup() {
    const Router = useRouter()
    const state = reactive({
      goodsList: [],
      pages: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        pagetotal: 0,
      }, //分页数据
      drawer: false,
    })

    // 获取商品列表
    const getGoodsList = async () => {
      const { pages } = state
      const {
        data: { goods: goodsList, total },
      } = await GET_GOODSLIST(pages)
      state.goodsList = goodsList
      state.pages.pagetotal = total
    }
    // 删除某个商品
    const handleDelete = async (id) => {
      const { meta } = await DELETE_GOODS(id)
      if (meta.status === 200) {
        ElMessage.success(meta.msg)
        // 刷新数据
        getGoodsList()
      } else ElMessage.error(meta.msg)
    }
    // 选择某一页
    const handleCurrentChange = (val) => {
      state.pages.pagenum = val
      getGoodsList()
    }
    // 前往添加商品页面
    const goAddGoodsPage = () => {
      Router.push({ path: '/goodsmang/goodslistup' })
    }
    // 搜素商品
    const searchText = ref('')
    const searchChange = () => {
      state.pages.query = searchText.value.trim()
      // 节流处理
      if (searchText.timer) clearTimeout(searchText.timer)
      searchText.timer = setTimeout(() => {
        // 执行请求渲染列表
        getGoodsList()
      }, 800)
    }
    // 编辑商品 跳转到编辑页
    const handleEditGoods = (id) => {
      if (id) Router.push({ path: '/goodsmang/goodslistup', query: { id } })
      else Router.push({ path: '/goodsmang/goodslistup' })
    }
    onMounted(() => {
      getGoodsList()
    })
    return {
      ...toRefs(state),
      searchText,
      handleCurrentChange,
      handleDelete,
      goAddGoodsPage,
      handleEditGoods,
      searchChange,
      formatDate,
    }
  },
}
</script>

<style lang="scss">
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.goods-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.table-head {
  margin: 0 0 20px;
}
.el-drawer {
  width: 50%;
}
.add-goods {
  .el-input {
    width: 80%;
  }
}
</style>
